using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows;
using System.Windows.Data;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Threading;
using LotroTweeter.Engine;

namespace LotroTweeter
{
    public partial class MainWindow : Window
    {

        public ConsoleText consoleText = new ConsoleText();
        public MainWindow()
        {
            InitializeComponent();
            ObjectDataProvider consoleData = FindResource("consoleData") as ObjectDataProvider;
            consoleData.ObjectInstance = consoleText;
        }

        private bool MinimizeTipShownOnce = false;
        private WindowState storedWindowState = WindowState.Normal;
        private void Window_StateChanged(object sender, EventArgs e)
        {
            if (WindowState == WindowState.Minimized)
            {
                ShowInTaskbar = false;
                if (notifyIcon != null && !MinimizeTipShownOnce)
                {
                    //ShowBalloonTip("The LOTRO Tweeter is still running. Click here to open the window again.");
                    MinimizeTipShownOnce = true;
                }
            }
            else
            {
                storedWindowState = WindowState;
                ShowInTaskbar = true;
            }
        }

        private void notifyIcon_MouseClick(object sender, MouseButtonEventArgs e)
        {
            if (e.ChangedButton == MouseButton.Left)
            {
                WindowState = storedWindowState;
            }
        }

        private LotroWatcher watcher;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            watcher = new LotroWatcher();
            new TwitterPublisher(consoleText, watcher);
            watcher.Start();
            consoleText.WriteLine("LOTRO Tweeter started");
        }

        //void watcher_BuildQualityChangeEvent(object sender, BuildStoreEventArgs buildWatcherEventArgs)
        //{
        //    ShowBalloonTip(String.Format("{0} changed the build quality to {1}", buildWatcherEventArgs.Data.LastChangedBy, buildWatcherEventArgs.Data.Quality));
        //}

        //private void ShowBalloonTip(string text)
        //{
        //    if (System.Threading.Thread.CurrentThread != notifyIcon.Dispatcher.Thread)
        //    {
        //        notifyIcon.Dispatcher.Invoke(DispatcherPriority.DataBind, new Action(() =>
        //                                                                                 {
        //                                                                                     notifyIcon.BalloonTipText = text;
        //                                                                                     notifyIcon.ShowBalloonTip(2000);
        //                                                                                 }));
        //    }
        //    else
        //    {
        //        notifyIcon.BalloonTipText = text;
        //        notifyIcon.ShowBalloonTip(2000);
        //    }
        //}

        //void watcher_BuildCompletionEvent(object sender, BuildStoreEventArgs buildWatcherEventArgs)
        //{
        //    var status = buildWatcherEventArgs.Data.Status;
        //    switch (status)
        //    {
        //        case BuildStatus.Succeeded:
        //            ShowBalloonTip("Build Succeeded");
        //            break;
        //        case BuildStatus.Failed:
        //            ShowBalloonTip("Build Failed");
        //            break;
        //        case BuildStatus.Stopped:
        //            ShowBalloonTip("Build Stopped");
        //            break;
        //        case BuildStatus.InProgress:
        //            ShowBalloonTip("Build Started");
        //            break;
        //    }
        //}

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            watcher.Stop();
        }
    }
}